//
//  BrowsePerseusViewController.m
//  BrowsePerseus
//
//  Created by Junghoon Jee on 11. 3. 10..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "BrowsePerseusViewController.h"
#import "PerseusData.h"

static enum {
	PICKER_WRITER,
	PICKER_WORK,
	PICKER_BOOK,
	PICKER_MODE_NUM,
} pickerMode;

@implementation BrowsePerseusViewController

@synthesize selectedWriter;
@synthesize selectedWork;

-(IBAction) onClickBackground {
	[lineField resignFirstResponder];
	[self checkShowButton];	
}

-(IBAction) onClickWriterButton {
	[lineField resignFirstResponder];

	pickerMode = PICKER_WRITER;
	[uiPicker reloadAllComponents];

	if ([uiPicker isHidden])
	{
		// Show writer picker
		[uiPicker setHidden:NO];
		[uiPicker selectRow:0 inComponent:0 animated:YES];
		
		// Set first writer
		self.selectedWriter = [g_pData getWriterForIndex:0];
		[writerButton setTitle:self.selectedWriter forState:UIControlStateNormal];	
	}
}

-(IBAction) onClickWorkButton {
	[lineField resignFirstResponder];
	if ([self.selectedWriter length] == 0)
		return;

	pickerMode = PICKER_WORK;
	[uiPicker reloadAllComponents];

	if ([uiPicker isHidden])
	{
		// Show writer picker
		[uiPicker setHidden:NO];
		[uiPicker selectRow:0 inComponent:0 animated:YES];
	}
	
	if (self.selectedWork == nil)
	{
		// Set first work
		self.selectedWork = [g_pData getWorkOfWriter: selectedWriter forRow:0];
		[workButton setTitle:self.selectedWork forState:UIControlStateNormal];
	} else{
		NSInteger rowForWork = [g_pData getIndexOfWork:selectedWork ofWriter:selectedWriter];		
		[uiPicker selectRow:rowForWork inComponent:0 animated:YES];
	}
}

-(IBAction) onClickBookButton {
	if ([selectedWork length] == 0)
		return;
	pickerMode = PICKER_BOOK;
}

-(IBAction) onInputLine {
	[self checkShowButton];
}

-(IBAction) onClickShowButton {
	NSString* url = [g_pData getURL:self.selectedWriter forWork:self.selectedWork inBook:selectedBook onLine:[[lineField text] intValue]];
	// NSLog(@"%@", url);
	[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
}

-(void) checkShowButton {
	if ([self.selectedWork length] && [self.selectedWriter length] && [[lineField text] length]) {
		[showButton setEnabled:YES];
	} else {
		[showButton setEnabled:NO];
	}
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[self checkShowButton];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
	return 1;
}

-(NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component {
	if (g_pData == nil)
		return 0;
	switch (pickerMode) {
		case PICKER_WRITER:
			return [g_pData getWriterCount];
		case PICKER_WORK:
			return [g_pData getWorkCountForWriter: selectedWriter];
	}
	return 0;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
	if (g_pData == nil)
		return @"";
	switch (pickerMode) {
		case PICKER_WRITER:
			if ([g_pData getWriterCount] > row)
				return [g_pData getWriterForIndex: row];
			break;
		case PICKER_WORK:
			if ([g_pData getWorkCountForWriter: selectedWriter] > row)
				return [g_pData getWorkOfWriter: selectedWriter forRow: row];
			break;
	}
	return @"";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
	switch (pickerMode) {
		case PICKER_WRITER:
			self.selectedWriter = [g_pData getWriterForIndex:row];
			[writerButton setTitle:self.selectedWriter forState:UIControlStateNormal];
			break;
		case PICKER_WORK:
			self.selectedWork = [g_pData getWorkOfWriter:self.selectedWriter forRow:row];
			[workButton setTitle:self.selectedWork forState:UIControlStateNormal];
			break;
	}
	[self checkShowButton];
}

@end
